package com.decent.ejfadmin.finance.dao.provider;

import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupplyDailySheet;
import com.decent.ejfadmin.utils.SQLWrapUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * 供货系统日报表sql管理
 *
 * @author hejunlei
 */
public class SupplyDailySheetDaoProvider {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplyDailySheetDaoProvider.class);

    /**
     * 计算期初余额查询消费明细
     *
     * @param params 参数
     * @return sql
     */
    public String queryUserSupplyConsumption(HashMap<String, Object> params) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("select uc.money,uc.balance from yuhuang_user_consumption uc where uc.userId = #{userId} and uc.type !=1  " +
                " and uc.startTime >= #{startTime} and uc.startTime < DATE_ADD(#{startTime},INTERVAL 1 DAY) " +
                "   order by uc.startTime ");
        String sort = (String) params.get("sort");
        sql.append(sort).append(",uc.id ").append(sort).append(" limit 0 , 1 ");
        return sql.toString();
    }

    /**
     * 日报表存库
     *
     * @param params 参数
     * @return sql
     */
    public String createDailySheet(HashMap<String, Object> params) {
        StringBuffer sql = new StringBuffer(100);
        StringBuffer value = new StringBuffer(100);
        SupplyDailySheet sheet = (SupplyDailySheet) params.get("sheet");
        try {
            Map<String, String> describe = BeanUtils.describe(sheet);
            sql.append("insert into yuhuang_supply_daily_sheet ( ");
            for (String s : describe.keySet()) {
                if (StringUtils.equals(s, "class") || StringUtils.equals(s, "id")) {
                    continue;
                }
                if (describe.get(s) != null) {
                    sql.append(s).append(",");
                    if (StringUtils.equals(s, "startTime")) {
                        value.append("'").append(describe.get(s)).append("'").append(",");
                        continue;
                    }
                    value.append(describe.get(s)).append(",");
                }
            }

            sql.append(") values ( ").append(value).append(") ");

            return StringUtils.replace(sql.toString(), ",)", ")");
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            LOGGER.error("创建日报表 {} 异常:{}", sql.toString(), e);
            return "select 90999 userId from dual";
        }
    }

    /**
     * 分页查询日报表
     *
     * @param params 参数
     * @return sql
     */
    public String querySupplyDailySheetPage(HashMap<String, Object> params) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("select ds.*,u.account userAccount,i.name userName from yuhuang_supply_daily_sheet ds,yuhuang_sup_user u,yuhuang_sup_info i where ds.userId=u.user_id " +
                "and u.user_id=i.user_id and startTime >= #{startTime} and startTime <= #{endTime} ");
        if (StringUtils.isNotBlank((String) params.get("userAccount"))) {
            sql.append(" and u.account = #{userAccount} ");
        }
        sql.append(" order by id  desc");
        Page page = (Page) params.get("page");
        if (page == null) {
            return sql.toString();
        }
        return SQLWrapUtils.wrapPage(sql, page.getBeginIndex(), page.getSize());
    }

    /**
     * 分页查询日报表计数
     *
     * @param params 参数
     * @return sql
     */
    public String querySupplyDailySheetCount(HashMap<String, Object> params) {
        StringBuffer sql = new StringBuffer(100);
        sql.append("select count(1) from yuhuang_supply_daily_sheet ds,yuhuang_sup_user u where ds.userId = u.user_id and startTime >= #{startTime} and startTime <= #{endTime} ");
        if (StringUtils.isNotBlank((String) params.get("userAccount"))) {
            sql.append(" and u.account = #{userAccount} ");
        }
        return sql.toString();
    }
}
